package com.coda.app.service.impl;

import com.coda.app.mapper.*;
import com.coda.app.service.LeaseAgreementService;
import com.coda.appVo.agreement.AgreementDetailVo;
import com.coda.appVo.agreement.AgreementItemVo;
import com.coda.appVo.graph.GraphVo;
import com.coda.context.BaseContext;
import com.coda.entity.*;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class LeaseAgreementServiceImpl implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentMapper apartmentMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private GraphMapper graphMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;


    /**
     * 获取个人租约基本信息
     * @return
     */
    @Override
    public List<AgreementItemVo> getList() {

        List<AgreementItemVo> agreementItemVoList = new ArrayList<>();

        List<LeaseAgreement> leaseAgreements = leaseAgreementMapper.getList(BaseContext.getCurrentId());
        for (LeaseAgreement leaseAgreement : leaseAgreements) {

            //获取房间图片
            List<GraphVo> graph = graphMapper.getGraph(leaseAgreement.getRoomId(), 2);
            //获取公寓信息
            ApartmentInfo apartment = apartmentMapper.getApartmentById(leaseAgreement.getApartmentId());
            //获取房间信息
            RoomInfo roomInfo = roomMapper.getRoomInfo(leaseAgreement.getRoomId());

            AgreementItemVo agreementItemVo = new AgreementItemVo();
            BeanUtils.copyProperties(leaseAgreement,agreementItemVo);

            agreementItemVo.setLeaseStatus(leaseAgreement.getStatus());
            agreementItemVo.setApartmentName(apartment.getName());
            agreementItemVo.setRoomGraphVoList(graph);
            agreementItemVo.setRoomNumber(roomInfo.getRoomNumber());

            agreementItemVoList.add(agreementItemVo);
        }
        return agreementItemVoList;
    }

    /**
     * 根据id获取租约详细信息
     * @param id
     * @return
     */
    @Override
    public AgreementDetailVo getDetail(Long id) {
        //获取租约基本信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.getDetail(id);
        //获取公寓信息
        ApartmentInfo apartment = apartmentMapper.getApartmentById(leaseAgreement.getApartmentId());
        //获取公寓图片列表
        List<GraphVo> graphApartment = graphMapper.getGraph(apartment.getId(), 1);
        //获取房间信息
        RoomInfo roomInfo = roomMapper.getRoomInfo(leaseAgreement.getRoomId());
        //获取房间图片列表
        List<GraphVo> graphRoom = graphMapper.getGraph(roomInfo.getId(), 2);
        //获取支付方式
        PaymentType paymentType = paymentTypeMapper.getPaymentById(leaseAgreement.getPaymentTypeId());
        //获取租期信息
        LeaseTerm leaseTerm = leaseTermMapper.getLeaseTermById(leaseAgreement.getLeaseTermId());

        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        BeanUtils.copyProperties(leaseAgreement,agreementDetailVo);
        agreementDetailVo.setApartmentName(apartment.getName());
        agreementDetailVo.setApartmentGraphVoList(graphApartment);
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());
        agreementDetailVo.setRoomGraphVoList(graphRoom);
        agreementDetailVo.setPaymentTypeName(paymentType.getName());
        agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());

        return agreementDetailVo;
    }

    /**
     * 保存或更新租约
     * @param leaseAgreement
     */
    @Override
    public void saveUpdate(LeaseAgreement leaseAgreement) {

        if (leaseAgreement.getId() == null ){
            leaseAgreementMapper.insert(leaseAgreement);
        }else {
            leaseAgreementMapper.update(leaseAgreement);
        }
    }




}
